﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class LocationWorkOrderListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void gvWorkOrder_Select(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyId = userIdentity.RSCompanyId;
            int WorkOrderId = Convert.ToInt32(e.CommandArgument.ToString());
            if (companyId > 0 && WorkOrderId > 0)
                Response.Redirect("~/RS/SBM/RSWorkOrderDetail.aspx?workorderid=" + WorkOrderId);
        }
        

        public void LoadLocationWorkOrderListByID(int companyid, int locationid)
        {
            hfLocationId.Value = locationid.ToString();
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT p.WorkOrderId,p.EstimateId,p.WorkOrderName,p.Description,p.Response,p.Active,p.StartDT,p.EndDT,(select t.typename from rs_tblType t where t.typeid=p.WorkOrderTypeId) as WorkOrderType,p.AssignTo,(select s.statusname from rs_tblStatus s where s.statusid=p.WorkOrderstatusid) as Status,p.ProjectId,p.LocationId,p.CompanyId, p.IsInvoiced FROM rs_tblWorkOrder p where p.CompanyId=@CompanyId and p.LocationId=@LocationId order by p.startdt, p.enddt desc";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@locationid";
            param.Value = locationid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvLocationWorkOrderList.DataSource = ds;
            gvLocationWorkOrderList.DataBind();
            myConnection.Close();

        }


        protected void gvWorkOrder_Delete(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int WorkOrderid = Convert.ToInt32(e.CommandArgument.ToString());
                if (RemoveWorkOrder(companyid, WorkOrderid))
                {
                    ucModalActionMessageControl.ShowModal("WorkOrder deleted succesfully!", GetRedirectURL(3), true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }


        private bool RemoveWorkOrder(int companyid, int WorkOrderid)
        {
            bool ret = false;
            if (companyid > 0 && WorkOrderid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@WorkOrderid", SqlDbType.Int, 0, ParameterDirection.Input, WorkOrderid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblWorkOrderTask WHERE workorderid=@WorkOrderid AND companyid=@companyid; DELETE FROM rs_tblWorkOrder WHERE workorderid=@WorkOrderid AND  CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                ret = true;
            }
            return ret;
        }

        protected void btnAddWorkOrder_Click(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;

                int locationid = 0;
                if (!String.IsNullOrEmpty(hfLocationId.Value))
                    locationid = Convert.ToInt32(hfLocationId.Value);
                if (companyid > 0 && locationid > 0)
                {
                    Response.Redirect("~/RS/SBM/RSManageWorkOrder.aspx?action=add&locationid=" + locationid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }


        private string GetRedirectURL(int activetab)
        {
            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("tabindex", activetab.ToString());
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            return url + updatedQueryString;
        }

    }

    
}